<template>
    <el-dialog :model-value="dialogVisible" :close-on-click-modal="true" :before-close="handleClose" :title="isAdd === 'add' ? '新增字段' : '修改字段'" append-to-body width="520px">
        <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="字段名称" prop="fieldName">
                <el-input v-model="form.fieldName" placeholder="请输入字段名称" />
            </el-form-item>
            <el-form-item label="字段code" prop="fieldCode">
                <el-input v-model="form.fieldCode" placeholder="请输入字段code"/>
            </el-form-item>
            <el-form-item label="字段类型" prop="fieldType">
                <el-select v-model="form.fieldType" filterable @change="changeFieldType" placeholder="请选择字段类型" class="w100p">
                    <el-option
                        v-for="item in fieldTypeOption"
                        :key="item.value"
                        :label="item.lable"
                        :value="item.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item 
                v-if="form.fieldType === 'String'" 
                label="字段长度" 
                :rules="[
                    {
                        required: true,
                        message: '请输入字段长度',
                        trigger: 'blur',
                    },
                    {
                        pattern: /^[+]{0,1}(\d+)$/,
                        message: '字段长度只能为正整数',
                        trigger: 'blur'
                    }
                ]" 
                prop="fieldLength">
                <el-input-number v-model="form.fieldLength" :min="1" placeholder="请输入字段长度" class="w100p"/>
            </el-form-item>
            <el-form-item 
                v-if="form.fieldType === 'Integer'" 
                label="字段长度" 
                :rules="[
                    {
                        required: true,
                        message: '请输入字段长度',
                        trigger: 'blur',
                    },
                    {
                        pattern: /^[+]{0,1}(\d+)$/,
                        message: '字段长度只能为正整数',
                        trigger: 'blur'
                    }
                ]" 
                prop="fieldLength">
                <el-input-number v-model="form.fieldLength" :min="1" :max="4294967295" placeholder="请输入字段长度" class="w100p"/>
            </el-form-item>
            <el-form-item 
                v-if="form.fieldType === 'decimal'" 
                label="字段长度" 
                :rules="[
                    {
                        required: true,
                        message: '请输入字段长度',
                        trigger: 'blur',
                    },
                    {
                        pattern: /^[+]{0,1}(\d+)[,][+]{0,1}(\d+)$/,
                        message: '请输入正确格式x,y(一共x位,小数最多Y位)',
                        trigger: 'blur'
                    },
                    { validator: validatedecimal, trigger: 'blur' }
                ]" 
                prop="fieldLength">
                <el-input v-model="form.fieldLength" placeholder="请输入字段长度x,y(一共x位,小数最多Y位)" class="w100p"/>
            </el-form-item>
            <el-form-item label="是否唯一" prop="only">
                <el-select v-model="form.only" filterable placeholder="请选择是否唯一" class="w100p">
                    <el-option
                        v-for="item in onlyOption"
                        :key="item.value"
                        :label="item.lable"
                        :value="item.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="描述信息" prop="desc">
                <el-input
                    v-model="form.desc"
                    :rows="3"
                    type="textarea"
                    placeholder="描述信息"
                />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="handleClose">取消</el-button>
                <el-button :loading="loading" type="primary" @click="submit()">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script  setup>
import { PREFIX } from '@/constant/const'
import { fieldTypeOption, onlyOption } from './config'
import { ElMessage } from 'element-plus'
// eslint-disable-next-line no-unused-vars
const props = defineProps({
    dialogVisible: {
        type: Boolean,
        default: false
    },
    isAdd: {
        type: String,
        default: 'add'
    },
    formData: {
        type: Object,
        default: ()=>{}
    }
})

watch(() => props.dialogVisible,(newVal) => {
    if(newVal === true){
        form.value = {...props.formData}
        if(form.value.fieldType === 'String'){
            form.value.fieldLength = Number(form.value.fieldLength)
        }
        if(form.value.fieldType === 'Integer'){
            form.value.fieldLength = Number(form.value.fieldLength)
        }
        if(form.value.fieldType === 'decimal'){
            form.value.fieldLength = form.value.fieldLength
        }
    }
})

const emit = defineEmits(['on-submit', 'handleClose'])
const formRef = ref(null)
const form = ref({
    only: false
})
const options = ref([])

const validatetypeCode = (rule, value, callback) => {
    let reg = /^[a-zA-Z]\w+$/
    let status = true
    const arr = PREFIX.validateLisOptions
    arr.forEach((item)=>{
        if(value === item){
            callback(new Error('不能为关键字'+item))
            status = false
        }
    })
    if (status) {
        if(!reg.test(value)){
            callback(new Error('26个英文字母和0-9的自然数+下划线组成，首字符为字母'))
        }else{
            callback()
        }   
    }
}

const validatedecimal = (rule, value, callback)=>{
    const decimalValue = value.split(',')
    if(Number(decimalValue[1]) >= Number(decimalValue[0])){
        callback(new Error('小数位最多不超过'+ decimalValue[0] +'位'))
    }else{
        callback()
    }
}

const rules = reactive({
    fieldName: [
        {
            required: true,
            message: '请输入字段名称',
            trigger: 'blur'
        },
        { max:50, message: '字段名称最长为50个字符', trigger: 'blur' },
    ],
    fieldCode: [
        {
            required: true,
            message: '请输入字段code',
            trigger: 'blur'
        },
        { max:50, message: '字段code最长为50个字符', trigger: 'blur' },
        { validator: validatetypeCode, trigger: 'blur' }
    ],
    fieldType: [
        {
            required: true,
            message: '请选择字段类型',
            trigger: 'blur'
        }
    ],
    only: [
        {
            required: true,
            message: '请选择是否唯一',
            trigger: 'blur'
        }
    ]
})

const loading = ref(false)
const handleClose = () => {
    formRef.value.resetFields()
    emit('handleClose',false)
}
const submit = () => {
    if (!formRef) return
    formRef.value.validate(valid => {
        if (valid) {
            if(form.value.status === false){
                form.value.status = false
            }else{
                form.value.status = true
            }
            emit('on-submit', {...form.value} , props.isAdd)
            handleClose()
        } else {
            return false
        }
    })
}

const changeFieldType = ()=> {
    if(form.value.fieldType === 'String'){
        form.value.fieldLength = 1
    }
    if(form.value.fieldType === 'Integer'){
        form.value.fieldLength = 1
    }
    if(form.value.fieldType === 'decimal'){
        form.value.fieldLength = ''
    }
}
</script>

<style lang="scss" scoped>
.dialog-footer{
    text-align: right;
}
</style>
